package com.only4play.user.utils.converter;

import com.only4play.common.PasswordGenerator;
import com.only4play.user.constant.UserPersonConstant;
import com.only4play.user.model.dto.input.PersonRegisterRequestDto;
import com.only4play.user.model.dto.output.UserPersonDto;
import com.only4play.user.model.dto.output.UserPersonalAccountDto;
import com.only4play.user.model.dto.output.PersonRegisterResponseDto;
import com.only4play.user.model.entity.UserPersonalAccount;
import com.only4play.user.model.entity.UserPerson;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserOrganizationConverter.java
 * @brief UserOrganizationConverter
 * @details UserOrganizationConverter
 * @date 2022-04-01
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-04-01                  liyuncong          Created
 */
//@Mapper
public class UserPersonConverter {

    public UserPersonalAccountDto fromEntity(UserPersonalAccount account) {
        if (account == null) {
            return null;
        }

        UserPersonalAccountDto userPersonalAccountDto = new UserPersonalAccountDto();

        userPersonalAccountDto.setUserPerson(userPersonToUserPersonDto(account.getPerson()));
        userPersonalAccountDto.setAccountId(account.getId());
        userPersonalAccountDto.setUsername(account.getUsername());
        userPersonalAccountDto.setAlias(account.getAlias());
        userPersonalAccountDto.setLoginType(account.getLoginType());
        userPersonalAccountDto.setIsDisable(account.getIsDisable());

        return userPersonalAccountDto;
    }

    public static UserPersonDto userPersonToUserPersonDto(UserPerson userPerson) {
        if (userPerson == null) {
            return null;
        }

        UserPersonDto userPersonDto = new UserPersonDto();

        userPersonDto.setPersonId(userPerson.getId());
        userPersonDto.setFullName(userPerson.getFullName());
        userPersonDto.setNickname(userPerson.getNickname());
        userPersonDto.setGender(userPerson.getGender());
        userPersonDto.setEmail(userPerson.getEmail());
        userPersonDto.setPhone(userPerson.getPhone());
        userPersonDto.setAuthStatus(userPerson.getAuthStatus());

        return userPersonDto;
    }

    public static UserPersonalAccount fromRegisterDto(
        PersonRegisterRequestDto requestDto,
        PasswordEncoder passwordEncoder,
        PasswordGenerator passwordGenerator
    ) {
        String salt = RandomStringUtils.randomAlphabetic(UserPersonConstant.SALT_LENGTH);
        String username = requestDto.getUsername();
        String password = requestDto.getPassword();
        String encodedPassword = passwordGenerator.generate(username, password, salt, passwordEncoder);

        UserPersonalAccount account = new UserPersonalAccount();
        account.setUsername(username);
        account.setPassword(encodedPassword);
        account.setAlias(requestDto.getFullName());
        account.setPasswordSalt(salt);
        account.setLoginType(UserPersonConstant.LOGIN_TYPE_DEFAULT);
        return account;
    }

    public static PersonRegisterResponseDto toRegisterResult(
        UserPerson userinfo, UserPersonalAccount account
    ) {
        PersonRegisterResponseDto responseDto = new PersonRegisterResponseDto();
        responseDto.setUserPersonId(userinfo.getId());
        responseDto.setUsername(account.getUsername());
        responseDto.setUserPersonalAccountId(account.getId());
        return responseDto;
    }

    public static UserPerson fromRegisterDto(PersonRegisterRequestDto requestDto) {
        UserPerson userinfo = new UserPerson();
        userinfo.setNickname(requestDto.getNickname());
        userinfo.setFullName(requestDto.getFullName());
        userinfo.setAuthStatus(UserPersonConstant.AUTH_STATUS_DEFAULT);
        return userinfo;
    }
}
